
package com.android.matmanual;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.SurfaceHolder.Callback;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.media.AudioManager;

public class GsensorView extends SurfaceView implements Callback,Runnable ,SensorEventListener{

	 
	public static final int TIME_IN_FRAME = 50; 

	
	Paint mPaint = null;
	Paint mTextPaint = null;
	SurfaceHolder mSurfaceHolder = null;

	
	boolean mRunning = false;

	Canvas mCanvas = null;

	
	boolean mIsRunning = false;
	
	
	private SensorManager mSensorMgr = null;    
	Sensor mSensor = null;    
	
	
	int mScreenWidth = 0;
	int mScreenHeight = 0;
	
	
	private int mScreenBallWidth = 0;
	private int mScreenBallHeight = 0;
	
	
	private Bitmap mbitmapBg;
	
	
	private Bitmap mbitmapBall;
	
	
	private float mPosX = 200;
	private float mPosY = 200;
	
	
	private float mGX = 0;
	private float mGY = 0;
	private float mGZ = 0;
	
	
	public GsensorView(Context context) {
	    super(context);

	}
	
	public GsensorView(Context context, AttributeSet attrs) {
		super(context);
		
	    this.setFocusable(true);
	    
	    this.setFocusableInTouchMode(true);
	    
	    mSurfaceHolder = this.getHolder();
	    
	    mSurfaceHolder.addCallback(this);
	    
	    mCanvas = new Canvas();
	    
	    mPaint = new Paint();
	    mPaint.setColor(Color.WHITE);
	   
	    mbitmapBall = BitmapFactory.decodeResource(this.getResources(), R.drawable.gsensor_ball);	    
	    mbitmapBg = BitmapFactory.decodeResource(this.getResources(), R.drawable.gsensor_bg);		    
	    mSensorMgr = (SensorManager) context.getSystemService(context.SENSOR_SERVICE);   
	    mSensor = mSensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);   	   
	    mSensorMgr.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);  
		
		
	
	}

	
	private void Draw() {
		
		if(mCanvas != null && mbitmapBg != null && mPaint != null && mbitmapBall != null)
		{
			mCanvas.drawBitmap(mbitmapBg,0,0, mPaint);	    
			mCanvas.drawBitmap(mbitmapBall, mPosX,mPosY, mPaint);
		}
	    
	    /*
	    mCanvas.drawText("X������ֵ ��" + mGX, 0, 20, mPaint);
	    mCanvas.drawText("Y������ֵ ��" + mGY, 0, 40, mPaint);
	    mCanvas.drawText("Z������ֵ ��" + mGZ, 0, 60, mPaint);
	    */
	}
	

	public void surfaceChanged(SurfaceHolder holder, int format, int width,
		int height) {

	}

	public void surfaceCreated(SurfaceHolder holder) {
	    
	    mIsRunning = true;
	    new Thread(this).start();
	    
	    mScreenWidth = GravityTest.ScreenWidth;
	    mScreenHeight = GravityTest.ScreenWidth;
	    
	    
	    mScreenBallWidth = mScreenWidth - mbitmapBall.getWidth();
	    mScreenBallHeight = mScreenHeight - mbitmapBall.getHeight();
	}

	public void surfaceDestroyed(SurfaceHolder holder) {
	    mIsRunning = false;
		mCanvas = null;	    
	    mPaint = null;
	    mSensorMgr.unregisterListener(this); 
		
		
	}

	public void run() {
	    while (mIsRunning) {
			long startTime = System.currentTimeMillis();	
			if(mCanvas != null && mbitmapBg != null && mPaint != null && mbitmapBall != null)
			{
				synchronized (mSurfaceHolder) {		    
					mCanvas = mSurfaceHolder.lockCanvas();
					Draw();		    
					if(mCanvas != null)
						mSurfaceHolder.unlockCanvasAndPost(mCanvas);
				}
			}
			long endTime = System.currentTimeMillis();		
			int diffTime = (int) (endTime - startTime);		
			while (diffTime <= TIME_IN_FRAME) {
				diffTime = (int) (System.currentTimeMillis() - startTime);		    
				Thread.yield();
			}

	    }

	}
	 
	public void onAccuracyChanged(Sensor arg0, int arg1) {
	    // TODO Auto-generated method stub
	    
	}

	public void onSensorChanged(SensorEvent event) {
	    mGX = event.values[SensorManager.DATA_X];
	    mGY= event.values[SensorManager.DATA_Y];
	    mGZ = event.values[SensorManager.DATA_Z];

	    mPosX -= mGX * 5;
	    mPosY += mGY * 5;
		
		Log.d("GsensorView", "############################# onSensorChanged mGX: "+mGX);
	    Log.d("GsensorView", "############################# onSensorChanged mGY: "+mGY);
		Log.d("GsensorView", "############################# onSensorChanged mGZ: "+mGZ);
		if(mGZ<-10)
		{
		
			
		}
	    if (mPosX < 0) {
			mPosX = 0;
	    } else if (mPosX > mScreenBallWidth) {
			mPosX = mScreenBallWidth;
	    }
	    if (mPosY < 0) {
			mPosY = 0;
	    } else if (mPosY > mScreenBallHeight) {
			mPosY = mScreenBallHeight;
	    }
	}
    }   
		 